// vite.config.js
import { defineConfig } from 'vite'
import { svelte } from '@sveltejs/vite-plugin-svelte'
import tailwind from 'tailwindcss'
import autoprefixer from 'autoprefixer'
import path from 'path'

// Tauri 后端地址配置
const tauriBackend = 'http://localhost:8080'

export default defineConfig({
  // 指定前端源代码目录
  root: './src',
  
  // 适配 Tauri 的公共路径
  publicDir: path.resolve(__dirname, './public'),
  
  plugins: [
    svelte({
      /* Svelte 插件配置 */
      emitCss: true,
      compilerOptions: {
        dev: process.env.NODE_ENV !== 'production',
        hydratable: true
      }
    })
  ],

  // CSS 处理配置 (Tailwind + PostCSS)
  css: {
    postcss: {
      plugins: [
        tailwind(path.resolve(__dirname, 'tailwind.config.js')), 
        autoprefixer()
      ]
    },
    preprocessorOptions: {
      scss: {
        additionalData: `@import '${path.resolve(__dirname, './src/styles/vars.scss')}';`
      }
    }
  },

  // 开发服务器配置 (适配 Tauri 热更新)
  server: {
    port: 5173,
    strictPort: true,
    proxy: {
      '/api': {
        target: tauriBackend,
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, '')
      }
    }
  },

  // 生产构建配置
  build: {
    outDir: path.resolve(__dirname, './dist'),
    emptyOutDir: true,
    sourcemap: process.env.NODE_ENV === 'development',
    rollupOptions: {
      output: {
        manualChunks: {
          tauri: ['@tauri-apps/api']
        }
      }
    }
  },

  // 路径别名配置
  resolve: {
    alias: {
      '@': path.resolve(__dirname, './src'),
      '@core': path.resolve(__dirname, './core')
    }
  }
})